package com.mdlib.helper.ui.fragments.rush.me;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.ServiceStartInfo;
import com.mdlib.helper.ui.adapter.ServiceStartAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/30 13:13
 * 邮箱：hqy_xz@126.com
 */

public class ServiceStartFragment extends BaseTitleFragment {


    @Bind(R.id.rv_sercive_list)
    RecyclerView mRvSerciveList;
    @Bind(R.id.sv_rush_start)
    SpringView mSvRushStart;
    private ServiceStartAdapter mServiceStartAdapter;


    private List<ServiceStartInfo> startInfoList = new ArrayList<>();
    private int pageNumber = 1;

    public static ServiceStartFragment newInstance() {
        return new ServiceStartFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_service_start;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.service_start_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    private void init() {


        mServiceStartAdapter = new ServiceStartAdapter(startInfoList);
        mRvSerciveList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvSerciveList.setAdapter(mServiceStartAdapter);
        mSvRushStart.setType(SpringView.Type.FOLLOW);
        mSvRushStart.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                startInfoList.clear();
                pageNumber = 1;
                getServiceStart();
            }

            @Override
            public void onLoadmore() {
                getServiceStart();

            }
        });
        mSvRushStart.setHeader(new HelperHeads(getActivity()));
        mSvRushStart.setFooter(new DefaultFooter(getActivity()));
        getServiceStart();

    }

    private void getServiceStart() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getServiceStart(pageNumber + "")
                .compose(RxHelper.<ArrayList<ServiceStartInfo>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<ServiceStartInfo>>() {
                    @Override
                    protected void onSucc(ArrayList<ServiceStartInfo> serviceStartInfos) {
                        pageNumber++;
                        updateList(serviceStartInfos);
                        mSvRushStart.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        mSvRushStart.onFinishFreshAndLoad();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    public void updateList(List<ServiceStartInfo> startInfos) {
        if (startInfos.size() > 0 && pageNumber >= 2) {
            for (ServiceStartInfo startInfo : startInfos) {
                startInfoList.add(startInfo);
            }
            mServiceStartAdapter.notifyDataSetChanged();
        } else if (startInfos.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        }
    }

}
